package sysu.el.array.ints.algors;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import sysu.el.array.ints.functors.FunBInt;


public class ArrFilterInt {
	
	static public int[] apply(int arr_num, int[] arr, FunBInt fun){
		int c = 0;
		for (int i=0; i<arr_num; i++){
			if (fun.apply(arr[i]))
				arr[c++] = arr[i]; 
		}
		int[] new_arr = new int[c];
		System.arraycopy(arr, 0, new_arr, 0, c);
		return new_arr;
	}
	
	static public int applyFast(int arr_num, int[] arr, FunBInt fun){
		int c = 0;
		for (int i=0; i<arr_num; i++){
			if (fun.apply(arr[i]))
				arr[c++] = arr[i];
		}
		return c;
	}

	static public int[] apply(int arr_num, int[] arr, Method m){
		int c = 0;
		for (int i=0; i<arr_num; i++){
			boolean b = true;
			try {
				b = (Boolean)(m.invoke(null, arr[i]));
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			if (b == true)
				arr[c++] = arr[i]; 
		}
		int[] new_arr = new int[c];
		System.arraycopy(arr, 0, new_arr, 0, c);
		return new_arr;
	}
}
